﻿using System;
using System.Text;
using fastCSharp;
using System.IO;
using fastCSharp.code.cSharp;

namespace diantou.web
{
    /// <summary>
    /// 网站生成配置
    /// </summary>
    public sealed class webConfig : fastCSharp.code.webConfig
    {
        /// <summary>
        /// Session数据
        /// </summary>
        public struct session
        {
            /// <summary>
            /// Session类型
            /// </summary>
            public enum type : int
            {
                /// <summary>
                /// 普通用户
                /// </summary>
                User,
                /// <summary>
                /// 考试用户
                /// </summary>
                ExamUser,
                /// <summary>
                /// 考试邀请 
                /// </summary>
                ExamInvitation,
            }
            /// <summary>
            /// 标识
            /// </summary>
            public int Id;
            /// <summary>
            /// Session类型
            /// </summary>
            public type Type;
        }
        /// <summary>
        /// Session类型
        /// </summary>
        public override Type SessionType
        {
            get { return typeof(session); }
        }
        /// <summary>
        /// 文件编码
        /// </summary>
        public override Encoding Encoding
        {
            get { return fastCSharp.encoding.Gb2312; }
        }
        /// <summary>
        /// 默认主域名
        /// </summary>
        public override string MainDomain
        {
            get { return diantou.dataModel.path.domain.Default.Domain; }
        }
        /// <summary>
        /// 静态文件网站域名
        /// </summary>
        public override string StaticFileDomain
        {
            get { return diantou.dataModel.path.domain.Default.StaticFileDomain; }
        }
        /// <summary>
        /// 图片文件域名
        /// </summary>
        public override string ImageDomain
        {
            get { return diantou.dataModel.path.domain.Default.ImageDomain; }
        }
        /// <summary>
        /// 轮询网站域名
        /// </summary>
        public override string PollDomain
        {
            get { return diantou.dataModel.path.domain.Default.PollDomain; }
        }
        /// <summary>
        /// 视图加载失败重定向
        /// </summary>
        public override string NoViewLocation
        {
            get { return "//" + diantou.dataModel.path.domain.Default.Domain + "/index.html"; }
        }
        /// <summary>
        /// Js引导文件加载毫秒数
        /// </summary>
        public override int JsLoadMilliseconds
        {
            get { return 20; }
        }
        /// <summary>
        /// 是否忽略大小写
        /// </summary>
        public override bool IgnoreCase
        {
            get { return true; }
        }
        /// <summary>
        /// 默认网站生成配置
        /// </summary>
        internal static readonly webConfig Default = new webConfig();
        /// <summary>
        /// 测试域名替换字符
        /// </summary>
        private static readonly byte testCharCode = diantou.dataModel.path.domain.Default.TestCharCode;
        /// <summary>
        /// 测试域名替换文件扩展名集合
        /// </summary>
        private static readonly subString[] extensionNames = new subString[] { "html", "js", "css" };
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="extensionName">文件名称信息</param>
        /// <param name="workPathLength">工作路径长度</param>
        /// <returns>文件内容</returns>
        internal static unsafe subArray<byte> ReadCacheFile(ref subString extensionName, int workPathLength, bool isFileCacheHeader)
        {
            if (testCharCode != 0)
            {
                if (extensionName.Value.Length > workPathLength && subString.Unsafe(extensionName.Value, workPathLength).Equals("favicon.ico"))
                {
                    return fastCSharp.net.tcp.http.domainServer.ReadCacheFile(extensionName.Value.Substring(0, workPathLength) + "testFavicon.ico", isFileCacheHeader);
                }
                foreach (subString name in extensionNames)
                {
                    if (name.Equals(extensionName))
                    {
                        subArray<byte> data = fastCSharp.net.tcp.http.domainServer.ReadCacheFile(extensionName.Value, isFileCacheHeader);
                        fixed (byte* dataFixed = data.UnsafeArray)
                        {
                            for (byte* start = dataFixed + data.StartIndex, end = start + Math.Max(data.Count - 4, 0); start != end; ++start)
                            {
                                if (*start == '5')
                                {
                                    if (*(int*)(start + 1) == ('1' + ('n' << 8) + ('o' << 16) + ('d' << 24)))
                                    {
                                        *++start = testCharCode;
                                        start += 3;
                                    }
                                }
                            }
                        }
                        return data;
                    }
                }
            }
            return default(subArray<byte>);
        }
        /// <summary>
        /// web试图404重定向
        /// </summary>
        internal static readonly webView.errorPath NotFound404 = new webView.errorPath { ErrorPath = diantou.dataModel.path.pub.NotFound404 };
    }
}
